home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / pl-sys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-18  |  2.7 KB  |  151 lines

  1. /*  $Id: pl-sys.c,v 1.16 1998/02/18 13:57:22 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6. */
  7.  
  8. #include "pl-incl.h"
  9.  
  10. #ifndef MAXVARNAME
  11. #define MAXVARNAME 1024
  12. #endif
  13.  
  14. word
  15. pl_shell(term_t command, term_t status)
  16. { char *cmd;
  17.  
  18.   if ( PL_get_chars(command, &cmd, CVT_ALL) )
  19.   { int rval = System(cmd);
  20.  
  21.     return PL_unify_integer(status, rval);
  22.   }
  23.   
  24.   return warning("shell/1: instantiation fault");
  25. }
  26.  
  27.  
  28. word
  29. pl_getenv(term_t var, term_t value)
  30. { char *n;
  31.  
  32.   if ( PL_get_chars(var, &n, CVT_ALL) )
  33.   { int len = getenvl(n);
  34.  
  35.     if ( len >= 0 )
  36.     { char *buf    = alloca(len+1);
  37.       
  38.       if ( buf )
  39.       { char *s;
  40.  
  41.     if ( (s=getenv3(n, buf, len+1)) )
  42.       return PL_unify_atom_chars(value, s);
  43.       } else
  44.     return PL_error("getenv", 2, NULL, ERR_NOMEM);
  45.     }
  46.  
  47.     fail;
  48.   }
  49.  
  50.   return warning("getenv/2: instantiation fault");
  51. }  
  52.  
  53.  
  54. word
  55. pl_setenv(term_t var, term_t value)
  56. { char *n, *v;
  57.  
  58.   if ( PL_get_chars(var, &n, CVT_ALL|BUF_RING) &&
  59.        PL_get_chars(value, &v, CVT_ALL) )
  60.   { Setenv(n, v);
  61.     succeed;
  62.   }
  63.  
  64.   return warning("setenv/2: instantiation fault");
  65. }
  66.  
  67.  
  68. word
  69. pl_unsetenv(term_t var)
  70. { char *n;
  71.  
  72.   if ( PL_get_chars(var, &n, CVT_ALL) )
  73.   { Unsetenv(n);
  74.  
  75.     succeed;
  76.   }
  77.  
  78.   return warning("unsetenv/1: instantiation fault");
  79. }
  80.  
  81.  
  82. word
  83. pl_argv(term_t av)
  84. { int n;
  85.   term_t head = PL_new_term_ref();
  86.   term_t list = PL_copy_term_ref(av);
  87.   int argc    = GD->cmdline.argc;
  88.   char **argv = GD->cmdline.argv;
  89.  
  90.   for(n=0; n<argc; n++)
  91.   { if ( !PL_unify_list(list, head, list) ||
  92.      !PL_unify_atom_chars(head, argv[n]) )
  93.       fail;
  94.   }
  95.  
  96.   return PL_unify_nil(list);
  97. }
  98.  
  99.  
  100. word
  101. pl_convert_time(term_t time, term_t year, term_t month,
  102.         term_t day, term_t hour, term_t minute,
  103.         term_t second, term_t usec)
  104. { double tf;
  105.  
  106.   if ( PL_get_float(time, &tf) && tf < PLMAXINT && tf > PLMININT )
  107.   { long t    = (long) tf;
  108.     long us   = (long)((tf - (double) t) * 1000.0);
  109.     struct tm *tm = LocalTime(&t);
  110.  
  111.     if ( PL_unify_integer(year,   tm->tm_year + 1900) &&
  112.      PL_unify_integer(month,  tm->tm_mon + 1) &&
  113.      PL_unify_integer(day,    tm->tm_mday) &&
  114.      PL_unify_integer(hour,   tm->tm_hour) &&
  115.      PL_unify_integer(minute, tm->tm_min) &&
  116.      PL_unify_integer(second, tm->tm_sec) &&
  117.      PL_unify_integer(usec,   us) )
  118.       succeed;
  119.   } else
  120.     warning("convert_time/8: instantiation fault");
  121.  
  122.   fail;
  123. }
  124.  
  125.  
  126. word
  127. pl_get_time(term_t t)
  128. { double stime;
  129. #ifndef HAVE_GETTIMEOFDAY
  130.   stime = (double)time((time_t *)NULL);
  131. #else
  132.   struct timeval tp;
  133.  
  134.   gettimeofday(&tp, NULL);
  135.   stime = (double)tp.tv_sec + (double)tp.tv_usec/1000000.0;
  136. #endif
  137.  
  138.   return PL_unify_float(t, stime);
  139. }
  140.  
  141.  
  142. word
  143. pl_sleep(term_t time)
  144. { double t;
  145.  
  146.   if ( PL_get_float(time, &t) )
  147.     Pause(t);
  148.   
  149.   succeed;
  150. }
  151.